package zz.note.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置交换机里消息的ttl过期时间
 * @Author: ZZ
 * @date 2022/2/11
 */
@Configuration
public class TTLRabbitConfig {
	@Bean
	public DirectExchange ttlDirectExchange() {
		return new DirectExchange("ttl_direct_exchange", true, false);
	}

	//1、配置队列模式
	@Bean
	public Queue ttlDirectQueue() {
		// 设置过期时间
		Map<String,Object> args  = new HashMap<>();
		args.put("x-message-ttl",10000);  //5秒，毫秒时间一定要是整形
		args.put("x-dead-letter-exchange","dead_direct_exchange");
		args.put("x-dead-letter-routing-key","dead");   //如果是fanout不需要配置路由key
		return new Queue("ttl.direct.queue", true,false,false,args);
	}
	@Bean
	public Binding bindingTTLChange() {
		return BindingBuilder.bind(ttlDirectQueue()).to(ttlDirectExchange()).with("ttl");
	}
	//2、配置消息模式
	@Bean
	public Queue ttlMessageDirectQueue() {
		return new Queue("ttl.message.direct.queue", true);
	}

	@Bean
	public Binding bindingTTLMessageChange() {
		return BindingBuilder.bind(ttlMessageDirectQueue()).to(ttlDirectExchange()).with("ttlmessage");
	}
}
